<?php
namespace app\admin\controller;
use app\admin\model\order;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
class Ddgl extends BaseController
{
    public function orderList(){
        //1接参，过滤
        $data = Request::get('','','trim,htmlspecialchars,strip_tags');

        //2调用模型类获取数据

        $list = \app\admin\model\order::getOrderList($data,$data);
        //3模板赋值
        $empty = "<tr><td colspan='8'>暂无数据！</td></tr>";
        View::assign('empty',$empty);
        View::assign($data);
        //4模板渲染
            View::assign('arr',$list[1]);
            View::assign('list',$list[0]);
        return View::fetch();
    }
    public function del(){
        $del = Request::get("del",'','trim,htmlspecialchars,strip_tags');
        if (''!==$del){
            $rt = order::destroy(function ($query) use($del){
                $query->where('order_id','in',$del);
            });
            if ($rt==1){
                $this->success("删除成功",url('orderList'));
            }else{
                $this->error('删除失败');
            }
        }
    }
    public function huishou()  //回收站
    {

        $data = Request::get('','','trim,htmlspecialchars,strip_tags');

        //2调用模型类获取数据

        $list = \app\admin\model\order::getOrderList($data,$data,false,true);
        //3模板赋值
        $empty = "<tr><td colspan='8'>暂无数据！</td></tr>";
        View::assign('empty',$empty);
        View::assign($data);
        //4模板渲染
        View::assign('arr',$list[1]);
        View::assign('list',$list[0]);
        return View::fetch();
    }


    public function huishoudel()  //回收站 //真实删除
    {
        $get = Request::get('','trim,htmlspecialchars,strip_tags');

        if(array_key_exists('oid',$get) && ''!==$get['oid']){
            $order = \app\admin\model\order::destroy($get['oid'],true);
            if($order){
                if($get['oid']){
                    unset($get['oid']);
                }
                if(''!==$get){
                    echo $this->success('删除成功！',url('huishou',$get));
                }else{
                    echo $this->success('删除成功！','huishou');
                }
            }else{
                echo $this->error('删除失败！');
            }
        }else{
            echo $this->error('参数错误！');
        }
    }
    public function recover()  //回收站 //数据恢复
    {
        $oid = Request::get('oid','');

        if(''!==$oid){
            $order = \app\admin\model\order::onlyTrashed()->find($oid);
            $order->restore();
            if($order){
                echo $this->success('数据恢复成功！','huishou');
            }else{
                echo $this->error('数据恢复失败！');
            }
        }else{
            echo $this->error('参数错误！');
        }

    }
//    public function orderList1(){
////        $buyer_name=Request::get('buyer_name','','trim,htmlspecialchars,strip_tags');
//        $order_amounts=Request::get('order_amounts','','trim,htmlspecialchars,strip_tags');
//        $order_amountss=Request::get('order_amountss','','trim,htmlspecialchars,strip_tags');
//        $cid=Request::get('cid',[],'trim,htmlspecialchars,strip_tags');
//        $order_sn=Request::get('order_sn','','trim,htmlspecialchars,strip_tags');
//        $field=[
//            'order_id','buyer_name','order_sn','order_amount','status','FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i:%S")'=>'add_date','postscript',
//            'status_id','s.order_status',
//        ];
//        $db=Db::table('ecm_order')
//            ->alias('o')
//            ->join(['ecm_order_status'=>'s'],'status=status_id','LEFT')
//            ->field($field)
//            ->order('add_time','desc');
//
//        //订单号搜索
//        if (''!==$order_sn){
//            $db->where('order_sn','like','%'.$order_sn.'%');
//        }
//        View::assign('order_sn',$order_sn);
//        //价格
//        if (''!==$order_amounts){
//            $db->where('order_amount','>=',$order_amounts);
//        }
//        View::assign('order_amounts',$order_amounts);
//        if (''!==$order_amountss){
//            $db->where('order_amount','<=',$order_amountss);
//        }
//        View::assign('order_amountss',$order_amountss);
//
//        if(!empty($cid)){
//            $db->whereIn('status',$cid);
//        }
//        View::assign('cid',$cid);
//        $arr=[
//            'list_rows'=>5,
//            'query'=>Request::instance()->param(),
//        ];
//        $agg=$db->paginate($arr);
//        $page = $agg->render();
//        View::assign('page',$page);
//        $order=$agg->toArray()['data'];
//        View::assign('order',$order);
//        $orders=Db::table('ecm_order_status')
//            ->field(true)
//            ->select()->toArray();
//
////        // 定义一个空数组
////        $prop = [];
////        // foreach 循环ecm_order_status表 取出值
////        foreach ($orders as $v){
////            // 数组降为用空数组接值 键，在赋值
////            $prop["{$v['status_id']}"] = $v['order_status'];
////        }
////        // 循环ecm_order表 取出值
////        foreach ($order as $kk=>$vv){
////            // 取ecm_order表里的$vv名字叫status值
////            // 在找到上一个空数组降为的status的值，赋给$vv
////            $vv['status']=$prop["{$vv['status']}"];
////            // 在数组降为，降$order，连接键名，值用$vv赋过去
////            $order["{$kk}"] = $vv;
////        }
//
//
//        View::assign('orders',$orders);
//        $empty = "<tr><td colspan='11'>暂无数据！</td></tr>";
//        View::assign('empty',$empty);
//
//        return View::fetch();
//    }
}